package com.katsu.myapplib.secret;

import java.security.PrivateKey;
import java.security.PublicKey;

public class SecretRSA implements Secret<String, String> {

    private PrivateKey v;

    private PublicKey e;

    public SecretRSA(String publicPem, String privatePem) {

        try {

            if(publicPem.contains(".pem")){

                e = r.ic(AppApplication.INSTANCE.getResources().getAssets().open(publicPem));

            } else {

                e = r.ic(publicPem);

            }

            if (privatePem.contains(".pem")) {

                v = r.te(AppApplication.INSTANCE.getResources().getAssets().open(privatePem));

            } else {

                v = r.te(privatePem);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    @Override
    public String encrypt(String plaintext) throws Exception {

        return k.ee(r.efd(plaintext.getBytes(), e));

    }

    @Override
    public String decrypt(String ciphertext) throws Exception {

        byte[] b = r.dfd(k.de(ciphertext), v);

        return b != null ? new String(b) : ciphertext;

    }

}
